# coding=utf-8
import time
import traceback
from random import randrange

from selenium import webdriver

index = 0
def getIndex():
    global index
    ++index
    return index


while True:
    try:
        firefox_option = webdriver.FirefoxOptions();
        firefox_option.headless = True
        profile = webdriver.FirefoxProfile()
        profile.set_preference("general.useragent.override",
                               "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36- " + str(
                                   randrange(1000000)) + "(KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36")
        browser = webdriver.Firefox(firefox_options=firefox_option, firefox_profile=profile)

        for i in range(40):
            pid = 153537 + randrange(800);
            browser.get('https://huishou.jd.com/detail?pid=' + str(pid))
            #all_cookie = "shshshfpa=1767cad3-ec8e-38f4-8451-ceab12a57c6d-1536456040; shshshfpb=1a2df9b4186e346e09f9c0c0e562753c70822cdb6f6622a8e5af7dacba; ipLoc-djd=2-2834-51983-0.137663065; ipLocation=%u4e0a%u6d77; unpl=V2_ZzNtbREAFBMmWhEBL01ZA2IKRV8SAEQcdw0RV3obDwxhUBZUclRCFX0UR1dnGVsUZAMZXUZcRhZFCEZkexhdBGQHE1tLX3MldDhFVEsRbAVjABJcR1BKEH0PT1R7EFsNbgUbXEBncxJFOE5XfhxdBm4FDl5DV0sTdDhHZHopXAVgBRtVS1ZLFUVDKFU2GVgGZwIXWktSSxJ8CEZdfBFVA24CEG1DZ0A%3d; __jdv=122270672|kong|t_1000250789_|tuiguang|bff7bcfdee4748f3af6834f203b87b58|1550322944011; user-key=76ad140e-cef7-401e-bc04-f4b9a4351bf5; __jdc=122270672; __jdu=1521046849681186987803; PCSYCityID=2; cn=0; mt_xid=V2_52007VwMWUV1ZV10WTBFbDGcDG1VVUVRTHkspWAFlCkcGWQhODh5AHEAAZwAWTlVcUVMDTE1dADUHEAFZXlUJL0oYXA17AhBOXFpDWhxCHF4OZgAiUG1YYlkdSBheBWMBE2JdXVRd; __jda=122270672.1521046849681186987803.1521046850.1551609720.1551614871.133; pin=kongyx1234; _tp=zrOvyuBE5N%2FiYMEscpr73g%3D%3D; _pst=kongyx1234; TrackID=1uO12FiLC1_YYaAese_Zdm-FquB8wlOtsSQgtAywZRpF_AiGF2JEH32OVbSReQJVeUGhqPpBFrqOoUR5XPWOxbFeEyM0YTwAtINOnduwNMROvQL9mRD4wiRBA2bJFHFD8|||HWSQFWT5HpPOHvi-N9tfvg; unick=kongyx1234; pinId=HWSQFWT5HpPOHvi-N9tfvg; shshshfp=d55d4c44dc8885bc56e3e8048df13a9f; 3AB9D23F7A4B3C9B=VHA7MEJD6KYNIU5QYD2O2O6VIJKCI5MA5SCCVSGGWZFEEN6ZPCFSDYQ3IOVHVFIS7DYX3MVZ2P63NJQJQFNYXV34BI; shshshsID=c6b37b8c9da349c545cccff6134fbf8a_3_1551615560808; thor=DE44FF211C579BC4C4C4CE982B43750B8E2A6E59A6BEC43CA14AC26EC9E3B14416762AFECAC46775C3857800331B1008C24C98C46EF0435CA9B3D420358DA8D55F70805106674D73BA2BAE533DC6FF767B0D05346D027DC06EBD81B77145937D2C13D620DBC11850C9B1C62B4CD7AEDDEF71CE0B8FB2A2E882125A3F027487820D47F64EC1FA24541F18329030C156CA; __jdb=122270672.12.1521046849681186987803|133.1551614871";
            all_cookie = '__jda=122270672.15515885365751738121427.1551588537.1551588537.1551916648.2; __jdb=122270672.8.15515885365751738121427|2.1551916648; __jdc=122270672; __jdu=15515885365751738121427; __jdv=122270672|direct|-|none|-|1551588536575; _pst=kongyx1234; _tp=zrOvyuBE5N%2FiYMEscpr73g%3D%3D; ceshi3.com=000; cn=0; PCSYCityID=2; pin=kongyx1234; pinId=HWSQFWT5HpPOHvi-N9tfvg; shshshfp=8bba1c842f504c0b5d1cd3608ef0b1cb; shshshfpa=96650dd0-0da5-50f5-b6f2-430f28471ca0-1551588551; shshshfpb=qHtRh0IqMhKPxKHrOMgthVw%3D%3D; shshshsID=388738dcc2b926247c30ddac646cadaa_4_1551916761885; thor=0003DC877440606F50CA48B731EE4F6E241B7E6E365CB0E94FCA1643F38394726D1F4D5CD43CA727851FD83A74EEDD7B8EC9C67E015FFFCEAFA779AC4389144A3258172FD02D609FE3DFE9E39F6D0D85D7253E9D57A817FA9BEEBA67DC9DCA3EF410E2BDCE772B4FD15F8F6D69530232B4F910028EACC6FD6329D4D88D3AA9D3722D90B080DE782EE2E1E1BBC5F5C2E4; TrackID=1yVXWhgtEL1D4jon8CzSJkofLMIVT8cWp-CHDakecGleJKAefnf0ENx7i6L0BAhQLaAN5nNSDIfU0KGmB_0QGtoDv7sViGoPqm0zrR7XvmdtjHo27crPBEohY37Zvtv9W; unick=kongyx1234; user-key=92b2ea02-46ba-4ffb-90c1-777cf4c95b07';
            cookies = all_cookie.split("; ")
            for cookie_str in cookies:
                cookie = cookie_str.split("=")
                browser.add_cookie({"name": cookie[0], "value": cookie[1], "domain": "jd.com"})

            container = browser.find_element_by_class_name("select-property")
            type_list = container.find_elements_by_tag_name("dl")
            for type in type_list:
                attrs = type.find_elements_by_css_selector("li.property-value")
                index = randrange(len(attrs))
                attrs[index].click()
            submitBtn = browser.find_element_by_class_name("submit-btn")
            submitBtn.click()
            time.sleep(1)
            print str(pid) + " success\n";
        browser.quit()
    except Exception as e:
        traceback.print_exc()
        browser.quit()



